#!/bin/bash

PROTO_TARGET_PATH="hundsun.com"

function cleanTempDir() {
    if [ -d ${PROTO_TARGET_PATH} ]; then
        rm -rf ${PROTO_TARGET_PATH}
    fi
}

function compile_proto() {
    for file in $(find . -name "*.proto" | grep -v vendor); do
        protos_dir=${file%/*}
        filename=${file##*/}
        echo "protoc --proto_path=$protos_dir --go_out=plugins=grpc:. ${protos_dir}/${filename} -I protos"
        protoc --proto_path=$protos_dir --go_out=plugins=grpc:. ${protos_dir}/${filename} -I protos
    done
}

function after_compile() {
    for pbfile in $(find ${PROTO_TARGET_PATH} -name "*.pb.go" | grep -v vendor); do
        prefix=${pbfile%/*}
        targetdir=${prefix#*hschain/}
        mv ${pbfile} ${targetdir}
    done

    cleanTempDir
}

function main() {
    cleanTempDir

    compile_proto

    after_compile
}

main
